<template>
	<view>
        <!-- 搜索框 -->
        <view class="search-box">
            <uni-search-bar @input="input" :radius="20" cancelButton="none"></uni-search-bar>
        </view>
        
        <!-- 搜索建议列表 -->
        <view class="sugg-list" v-if="searchResults.length !== 0">
            <view class="sugg-item" @click="gotoDetail(item)" v-for="(item,index) in searchResults" :key="index">
                <view class="goods-name">{{item.goods_name}}</view>
                <uni-icons type="arrowright" size="16"></uni-icons>
            </view>
        </view>
        
        <!-- 搜索历史区域 -->
        <view class="history-box" v-else>
            <!-- 标题区域 -->
          <view class="history-title">
               <text>搜索历史</text>
               <uni-icons type="trash" size="17" @click="clean"></uni-icons>
          </view> 
          <!-- 列表区域 -->
          <view class="history-list">
              <uni-tag :text="item" @click=gotoGoodsList(item) v-for="(item,index) in historyList" :key="index"></uni-tag>
          </view>
        </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null, //防抖延时器
                kw: '', //输入框的值
                searchResults: [], //搜索的结果列表
                historyList: [], //搜索历史列表
			}
		},
        
        onLoad() {
            this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
        },
        
        methods: {
            // input框输入事件处理函数
            input(e) {
                // 清除定时器
                clearTimeout(this.timer)
                this.timer = setTimeout( () => {
                    this.kw = e.value
                    this.getSearchList()
                },500)
            },
            
            // 获取搜索建议列表数据请求函数
            async getSearchList() {
                // 判断关键字是否为空 
                if(this.kw.length === 0) {
                    this.searchResults = []
                    return
                }
                try{
                    const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
                    this.searchResults = res.message
                    this.saveSearchHistory()
                }catch(e){
                    uni.$showMsg()
                }
            },
       
            // 搜索建议项的点击跳转事件
            gotoDetail(item) {
                uni.navigateTo({
                    url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
                })
            },
            
            // 存储搜索记录方法
            saveSearchHistory() {
                
               const flag = this.historyList.some((item) => {
                    return item === this.kw
                })
                
                if(flag === true) {
                    const index = this.historyList.findIndex((item)=> {
                        return this.kw === item
                    })
                    this.historyList.splice(index,1)
                    this.historyList.unshift(this.kw)
                } else {
                    this.historyList.unshift(this.kw)
                }
                
                // 对搜索历史数据进行持久化的存储
                uni.setStorageSync('kw',JSON.stringify(this.historyList))
            },
            
            // 清除历史记录
            clean() {
                this.historyList = []
                uni.setStorageSync('kw', '[]')
            },
            
            // 搜索历史标签跳转页面事件
            gotoGoodsList(kw) {
                uni.navigateTo({
                    url: '/subpkg/goods_list/goods_list?query=' + kw
                })
            }
        }
	}
</script>

<style lang="scss">
    .search-box {
        position: sticky;
        top: 0;
        z-index: 999;
    }
    
    .sugg-list {
        padding: 0 5px;
        
        .sugg-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
            padding: 13px;
            border-bottom: 1px solid #efefef;
            
            .goods-name {
                // 强制一行显示
                white-space: nowrap;
                // 溢出隐藏
                overflow: hidden;
                // 文本溢出后,使用...代替
                text-overflow: ellipsis;
                margin-right: 3px;
            }
        }
    }
    
    .history-box {
        padding: 0 5px;
        .history-title {
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            border-bottom: 1px solid #efefef;
        }
        
        .history-list {
            display: flex;
            flex-wrap: wrap;
            uni-tag {
                margin-top: 5px;
                margin-right: 5px;
            }
        }
    }
</style>
